package str;

import common.Node;

/**
 * create by yongli on 2021-07-05 07:51
 * 字符串翻转， 算法习题
 * 有很多种实现，比如两个栈倒一下
 */

public class Reverse {

    public static void main(String[] args) {
        String str = "abcdefg";
        int k=12;
        System.out.println(reverse(str,k));
    }

    public static int reverse(String s,int k) {

        if(s.length()<k){
            return 0;
        }
        if(k==1){
            return s.length();
        }
        int  max =0;
        String s0 = s.substring(0,k);
        for(int i=0;i<k;i++){
            if(s0.charAt(0)!= s0.charAt(i)){
                max =0;
                s0="";
                break;
            }
            max = k;
        }
        for(int i=k;i<s.length();i++){
            //  包含它
            if(s0.contains(""+s.charAt(i))){
                s0= s0+s.substring(i-1,i);
                if(max<s0.length()){
                    max = s0.length();
                }
            }
        }
        return max;
    }

    public Node ReverseList(Node head) {
        if (head == null) {
//            return head;
            // 更直接的返回这个值，而不是让开发者去催促
            return null;
        }
        Node second = head.next;

        while (second != null) {
            Node temp = second.next;
            second.next = head;
            // 难道是出现了环？
            head = second;
            second = temp;

        }
        return head;
    }
}
